package com.tnovoselec.android.travelassistant.overlay;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;
import com.tnovoselec.android.travelassistant.Constant;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;
import com.tnovoselec.android.travelassistant.view.CustomMapView;

public class BankomatiOverlay extends AbstractMapOverlay<IGeoPoint> {
	public interface BankomatSelectedListener {
		public void onBankomatSelected();
	}

	private Drawable hpbMarker;
	private Drawable zabaMarker;
	private Drawable pbzMarker;
	private Drawable otpMarker;
	private Drawable rbaMarker;
	private Drawable splitskaMarker;
	private Drawable ersteMarker;
	private Drawable hypoMarker;

	private BankomatSelectedListener listener;

	public BankomatiOverlay(Context context, Drawable defaultMarker, List<IGeoPoint> list, CustomMapView mapview) {
		super(boundCenter(defaultMarker));

		this.mContext = context;

		hpbMarker = context.getResources().getDrawable(R.drawable.hpb_icon);
		zabaMarker = context.getResources().getDrawable(R.drawable.zaba_icon);
		pbzMarker = context.getResources().getDrawable(R.drawable.pbz_icon);
		otpMarker = context.getResources().getDrawable(R.drawable.otp_icon);
		rbaMarker = context.getResources().getDrawable(R.drawable.rba_icon);
		splitskaMarker = context.getResources().getDrawable(R.drawable.splitska_icon);
		ersteMarker = context.getResources().getDrawable(R.drawable.erste_icon);
		hypoMarker = context.getResources().getDrawable(R.drawable.hypo_icon);

		this.items = list;
		overlayItems = new ArrayList<OverlayItem>();
		if (list != null) {
			for (IGeoPoint bankomat : list) {
				BankomatOverlayItem item = new BankomatOverlayItem(new GeoPoint((int) (bankomat.getLatitude() * 1e6), (int) (bankomat.getLongitude() * 1e6)),
						bankomat.getGrad() + " " + bankomat.getAdresa(), bankomat.getBank_id(), bankomat.getBank_id());
				item.setMarker(defaultMarker);
				overlayItems.add(item);
			}
		}

		popupView = LayoutInflater.from(context).inflate(R.layout.map_popup, null);
		popupView.setOnClickListener(this);
		this.mapview = mapview;
		populate();
	}

	@Override
	protected boolean onTap(int index) {
		super.onTap(index);

		if (listener != null) {
			listener.onBankomatSelected();
		}

		return true;
	}

	private class BankomatOverlayItem extends OverlayItem {

		private String type;

		public BankomatOverlayItem(GeoPoint point, String title, String snippet, String type) {
			super(point, title, snippet);
			this.type = type;
		}

		@Override
		public Drawable getMarker(int stateBitset) {
			Drawable drawable = super.getMarker(stateBitset);
			if (type.toLowerCase().contains(Constant.HPB_REGEX)) {
				drawable = hpbMarker;
			} else if (type.toLowerCase().contains(Constant.PBZ_REGEX)) {
				drawable = pbzMarker;
			} else if (type.toLowerCase().contains(Constant.ZABA_REGEX)) {
				drawable = zabaMarker;
			} else if (type.toLowerCase().contains(Constant.ERSTE_REGEX)) {
				drawable = ersteMarker;
			} else if (type.toLowerCase().contains(Constant.OTP_REGEX)) {
				drawable = otpMarker;
			} else if (type.toLowerCase().contains(Constant.SPLITSKA_REGEX)) {
				drawable = splitskaMarker;
			} else if (type.toLowerCase().contains(Constant.HYPO_REGEX)) {
				drawable = hypoMarker;
			} else if (type.toLowerCase().contains(Constant.RBA_REGEX)) {
				drawable = rbaMarker;
			}
			return boundCenter(drawable);
		}
	}

	public void setListener(BankomatSelectedListener listener) {
		this.listener = listener;
	}

}
